package com.sllp.C71S3TsllpShop.web;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Comment;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.CommentBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersItemBizImpl;
import com.sllp.C71S3TsllpShop.dao.CommentMapper;

@Controller
public class CommentAction {

	@Resource
	private CommentBizImpl cbiz;
	
	@Resource
	private CarBizImpl carbiz;
	
	@Resource
	private CommentMapper cm;
	
	@Resource
	private OrdersItemBizImpl obiz;
	
	/**
	 * 我的评论显示
	 * @param m
	 * @return
	 */
	@GetMapping("comment")
	public String toComment(HttpSession session,@SessionAttribute("btlist") List<List<BigType>> lists,
			Model m,String pre,String next,
			@SessionAttribute("loginUser") User user,
			@SessionAttribute(name="page",required=false)Integer page) {
		m.addAttribute("btlist", lists);
		if(next==null){
			page=1;
		}else if(Integer.valueOf(next)>0){
			page=Integer.valueOf(next);
		}
		if(pre!=null){
			page=1;
		}
		PageHelper.startPage(page,5);
		//从新设置分页的页码，相当于page++
	    m.addAttribute("page", page);
	    m.addAttribute("pag", page-1);
	    //显示所有评论
		List<Comment> clist=cbiz.findComment(user);
		m.addAttribute("commentList", clist);
		
		//右上角购物车显示
//		List<Car> carlist=carbiz.findCar(user);
//		m.addAttribute("carlist", carlist);
		return "Member_Comment";
	}
	
	/**
	 * 写评论
	 */
	@GetMapping("WriteComment")
	public String WriteComment(Model m,String id,Integer pid){
		//根据订单详情id查询商品
		OrdersItem list=obiz.findpro(id);
		m.addAttribute("oitemlist", list);
		
		//根据商品id查评论
	    List<Comment> clist=cbiz.findCommentByPro(pid);
	    m.addAttribute("commentList", clist);
		return "Member_Msg";
	}
	/**
	 * 提交评论
	 */
	@ResponseBody
	@PostMapping("add")
	public ModelAndView addComment(Comment comment,@SessionAttribute("loginUser") User user){
		Timestamp time= new Timestamp(System.currentTimeMillis());
		comment.setUser(user.getId());
		comment.setSendTime(time);
		cbiz.addComment(comment);
		//提交评论后订单详情状态改变
		obiz.updateOrderState(comment.getOrdersItem());
		return new ModelAndView("redirect:comment");
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=carbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
